﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using PA3PC113.Untity;

namespace PA3PC113
{
    public class basPA3PC113_2
    {
        public Makelog log = new Makelog();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strMC"></param>
        /// <param name="strPcd"></param>
        /// <param name="DouCtime"></param>
        public void GetCureParameterItem(string strMC, string strPcd, out double DouCtime)
        {
            int intOpenflag = 0;
            long RetryOK_NG = 0;
            string strPRESSTYPE = "";
            strMC = strMC.ToString().Trim();
            DouCtime = 0;
            DataTable dt1 = new DataTable();
            DataTable dt2 = new DataTable();
            try
            {
                string sSQL = "select PRESSTYPE from CurePressMaster where CurePress = " + strMC + "";
                intOpenflag = 0;
                dt1 = DBUtilityOracle.DBUtilityOra.QueryTable(sSQL.ToString());
                //Set OraDynaset = OraDatabase.CreateDynaset(sSQL, 0&)
                intOpenflag = 1;
                if (dt1.Rows.Count>0)
                {
                      strPRESSTYPE =dt1.Rows[0]["PRESSTYPE"].ToString();
                }  

               string sSQL1 = "select TOTALCURETIME from CureParameter where ProductionCode = " + strPcd + " and PRESSTYPE = " + strPRESSTYPE + "";
                 intOpenflag = 0;
                 dt2 = DBUtilityOracle.DBUtilityOra.QueryTable(sSQL1.ToString());
                 intOpenflag = 1;

                 if (dt2.Rows.Count > 0)
                 {
                     DouCtime =double.Parse(dt2.Rows[0]["TOTALCURETIME"].ToString());

                 }
            }
            catch (Exception ex)
            {
                string bt = "GetCureParameterItem";
                string msgs = ex.ToString();
                log.MakeLog(2,bt,msgs);

            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="TcdRst"></param>
        public void UpdateCureCycleTimeMain(MesRst TcdRst)
        {
            int intCycle=0;
            long RetryOK_NG = 0;
            GetCureCycleTime(TcdRst,out intCycle);
            UpdateCureCycleTime(TcdRst, intCycle);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="TcdRst"></param>
        /// <param name="intCycle"></param>
        public void GetCureCycleTime(MesRst TcdRst, out int intCycle)
        {
            int intRet = 0;
            int intCtimeMin = 0;
            int intCtimeSec = 0;
            int intCureTime = 0;
            int intOpenSeg = 0;
            int intOpenFull = 0;
            string strRet = "";
            string strPcd = "";
            string strRev = "";
            string strMfullseg = "";
            long RetryOK_NG = 0;
            double DouCtime = 0;
            strPcd = TcdRst.Right_ProductionCode.Trim();
            if (strPcd == "")
            {
                strPcd = TcdRst.Left_ProductionCode.Trim();
            }

            try
            {
                int CureSheduleM_Found = GetCureSheduleMasterItem(strPcd, strRev, out intCtimeMin, out  intCtimeSec, out  strMfullseg);
                if (CureSheduleM_Found == 1)
                    intCureTime = intCtimeMin * 60 + intCtimeSec;
                else
                {
                    GetCureParameterItem(TcdRst.MachineNo, strPcd, out DouCtime);
                    intCureTime = int.Parse(DouCtime.ToString()) * 60;
                }

                GetCurePressMasterItem(TcdRst.MachineNo, out intOpenSeg, out intOpenFull);
                if (CureSheduleM_Found == 1)
                {
                    if (strMfullseg == "Z")
                        intCureTime = intCureTime + intOpenSeg;
                    else
                    {
                        intCureTime = intCureTime + intOpenFull;
                    }

                }
                else
                {
                    intCureTime = intCureTime + intOpenSeg;
                }

                intCycle = intCureTime;
            }
            catch (Exception ex)
            {
                intCycle = 0;
                string bt = "GetCureCycleTime";
                string msgs = ex.ToString();
                log.MakeLog(2, bt,msgs);
            }

            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strPcd"></param>
        /// <param name="strRev"></param>
        /// <param name="intCtimeMin"></param>
        /// <param name="intCtimeSec"></param>
        /// <param name="strMfullseg"></param>
        /// <returns></returns>
        public int GetCureSheduleMasterItem(string strPcd, string strRev, out int intCtimeMin, out int intCtimeSec, out string strMfullseg)
        {

            int intOpenflag = 0;
            long RetryOK_NG = 0;
            int CureSheduleM_Found = 0;
            DataTable dt = new DataTable();
            string sSQL = "select CureTimeMinute, CureTimeSecond, MoldFullSegID from CureScheDuleMaster where ProductionCode = '" + strPcd + "'";
            intOpenflag = 0;
            try
            {
                dt = DBUtilityOracle.DBUtilityOra.QueryTable(sSQL.ToString());
                intOpenflag = 1;

                if (dt.Rows.Count > 0)
                {
                    intCtimeMin = int.Parse(dt.Rows[0]["CureTimeMinute"].ToString());// OraDynaset.Fields("CureTimeMinute").Value
                    intCtimeSec = int.Parse(dt.Rows[0]["CureTimeSecond"].ToString());// OraDynaset.Fields("CureTimeSecond").Value
                    strMfullseg = dt.Rows[0]["MoldFullSegID"].ToString();// OraDynaset.Fields("MoldFullSegID").Value
                    CureSheduleM_Found = 1;
                }
                else
                {
                    intCtimeMin = 0;
                    intCtimeSec = 0;
                    strMfullseg = "0";
                }
            }
            catch(Exception ex)
            {
                CureSheduleM_Found = 0;
                intCtimeMin = 0;
                intCtimeSec = 0;
                strMfullseg = "0";
                string ms = "GetCuresheduleMasterItem";
                string msgg = ex.ToString();
                log.MakeLog(2, ms, msgg);

            }
            
            return CureSheduleM_Found;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strMC"></param>
        /// <param name="intOpenSeg"></param>
        /// <param name="intOpenFull"></param>
        public void GetCurePressMasterItem(string strMC, out int intOpenSeg, out int intOpenFull)
        {

            int intOpenflag = 0;
            long RetryOK_NG = 0;
            DataTable dt = new DataTable();
            strMC = strMC.Trim();
            string sSQL = "select OpenCloseTimeSeg, OpenCloseTimeFull from CurePressMaster where CurePress = " + strMC + "";

            intOpenflag = 0;
            try
            {
                dt = DBUtilityOracle.DBUtilityOra.QueryTable(sSQL.ToString());
                intOpenflag = 1;
                if (dt.Rows.Count > 0)
                {
                    intOpenSeg = int.Parse(dt.Rows[0]["OpenCloseTimeSeg"].ToString()); //.Fields("OpenCloseTimeSeg").Value
                    intOpenFull = int.Parse(dt.Rows[0]["OpenCloseTimeFull"].ToString());// OraDynaset.Fields("OpenCloseTimeFull").Value
                }
                else
                {
                    intOpenSeg = 0;
                    intOpenFull = 0;
                }
            }
            catch(Exception ex)
            {
                intOpenSeg = 0;
                intOpenFull = 0;
                string ms = "GetCurePressMasterItem";
                string msg = ex.ToString();
                log.MakeLog(2, ms, msg);
            }
            

        }
        public void UpdateCureCycleTime(MesRst TcdRst, int intCycle)
        {

            string dSQL = "";
            long lngRet = 0;
            long RetryOK_NG = 0;
            string sSQL = "";
            string MoldNoR = new string(' ',10);
            string MoldCleanSectorR = new string(' ',12);
            string MoldCleanSideR = new string(' ',12);
            string BladderChangeR = new string(' ',12);
            dSQL = dSQL + " NowProductionCodeR = '" + TcdRst.Right_ProductionCode.Trim() + "',";
            dSQL = dSQL + " SizeChangeR = '" + DateTime.Now.ToString("yyyyMMddhhmm") + "',";
            dSQL = dSQL + " CureCycleTime = '" + intCycle + "',";// & intCycle & ", "
            dSQL = dSQL + " StatusR = 4,";// & Chr(39) & "4" & Chr(39) & ", "
            dSQL = dSQL + " BladderUseR = 0,";// & ", "
            dSQL = dSQL + " MoldUseDaySectorR = 0,"; // ", "
            dSQL = dSQL + " MoldUseDaySideR = 0,"; //& ", "
            dSQL = dSQL + " MoldNoR = '"+MoldNoR+"',";
            dSQL = dSQL + " MoldCleanSectorR = '"+MoldCleanSectorR+"',";
            dSQL = dSQL + " MoldCleanSideR = '"+MoldCleanSideR+"',";
            dSQL = dSQL + " BladderChangeR =  '" + BladderChangeR + "'";
            sSQL = "UPDATE CuringMachineStatus SET ";
            sSQL = sSQL + dSQL;
            sSQL = sSQL + " WHERE CurePress = " + TcdRst.MachineNo + "";
            try
            { lngRet = DBUtilityOracle.DBUtilityOra.ExecuteSql(sSQL.ToString()); }
            catch(Exception ex)
            {
                string ms = "UpdateCureCycleTime:执行更细第一个异常";
                string mss = ex.ToString();
                log.MakeLog(2,ms,mss);
            }
            dSQL = "";
            sSQL = "";
            string MoldNoR1 = new string(' ',10);
            string MoldCleanSectorR1 = new string(' ',12);
            string MoldCleanSideR1 = new string(' ',12);
            string BladderChangeR1 = new string(' ',12);
            dSQL = dSQL + " NowProductionCodeL = '" + TcdRst.Left_ProductionCode.Trim() + "', ";
            dSQL = dSQL + " SizeChangeL = '" + DateTime.Now.ToString("yyyyMMddhhmm") + "', ";
            dSQL = dSQL + " CureCycleTime = '" + intCycle + "', ";
            dSQL = dSQL + " StatusL = 4, ";
            dSQL = dSQL + " BladderUseL = 0,";
            dSQL = dSQL + " MoldUseDaySectorL = 0,";
            dSQL = dSQL + " MoldUseDaySideL = 0,";
            dSQL = dSQL + " MoldNoL = '"+MoldNoR1+"',";
            dSQL = dSQL + " MoldCleanSectorL = '" + MoldCleanSectorR1 + "',";
            dSQL = dSQL + " MoldCleanSideL = '" + MoldCleanSideR1 + "',";
            dSQL = dSQL + " BladderChangeL =  '" + BladderChangeR1 + "'";

            sSQL = "UPDATE CuringMachineStatus SET ";
            sSQL = sSQL + dSQL;
            sSQL = sSQL + " WHERE CurePress = " + TcdRst.MachineNo + "";

            
            try
            { lngRet = DBUtilityOracle.DBUtilityOra.ExecuteSql(sSQL.ToString()); }
            catch (Exception ex)
            {
                string ms = "UpdateCureCycleTime:执行更细第二个异常";
                string mss = ex.ToString();
                log.MakeLog(2, ms, mss);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strPcd"></param>
        /// <param name="strRet"></param>
        public void GetRevisionNo(string strPcd,out string strRet)
        {
            int intOpenflag = 0;
            long RetryOK_NG = 0;
            DataTable dt1 = new DataTable();
            strRet = "";
            string sSQL = "";
            sSQL = "select RevisionNo from TireMasterProcess";
            sSQL = sSQL + " where";
            sSQL = sSQL + " ProductionCode = " + strPcd + "";
            sSQL = sSQL + " and";
            sSQL = sSQL + " FieldName = 'DateAdopted'";
            sSQL = sSQL + " and";
            sSQL = sSQL + " DataCHR <= " + DateTime.Now.ToString("yyyy/mm/dd") + "";
            sSQL = sSQL + " ORDER BY DataCHR DESC ";
            intOpenflag = 0;
            try
            {
                dt1 = DBUtilityOracle.DBUtilityOra.QueryTable(sSQL.ToString());
                intOpenflag = 1;
                if (dt1.Rows.Count > 0)
                {
                    strRet = dt1.Rows[0]["RevisionNo"].ToString();//OraDynaset.Fields(0).Value
                }
            }
            catch(Exception ex)
            {
                string bt = "GetRevisionNo";
                string mssg = ex.ToString();
                log.MakeLog(2, bt, mssg);
            }
            
        }
    }
}